/*
    Copyright 2015 <copyright holder> <email>

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/


#include "lcd_display.h"

lcd_display::lcd_display(string device_file)
{
	lcd_device_file = device_file;
}

lcd_display::~lcd_display()
{
	lcd_close();
}
void lcd_display::lcd_open()
{
	//非阻塞方式打开，如果打开错误，会立即返回  
    if((lcd_fd = open(lcd_device_file.c_str(), O_RDWR | O_NONBLOCK)) < 0)  
    {  
        perror("Fail to open");  
        exit(EXIT_FAILURE);  
    }   
}

void lcd_display::lcd_close()
{
	if(-1 == close(lcd_fd))
	{
		perror("faile to colse LCD");
		exit(EXIT_FAILURE);
	}
}

int lcd_display::clip(int value, int min, int max)
{
	return (value > max ? max : value < min ? min : value);
}

void lcd_display::lcd_show(int w,int h,void* addr)
{
	unsigned char* in=(unsigned char*)addr;
	int width=w;
	int height=h;
	int istride=width * 2;
	int x,y,j;
	int y0,u,y1,v,r,g,b;
	long location=0;

	for ( y = 0; y < height; ++y)//480
	{
		for (j = 0, x=0; j < width * 2; j += 4,x +=2)//640
		{
			//location:64, x:18, vinfo.xoffset:0, vinfo.bits_per_pixel/8:4, vinfo.yoffset:0, finfo.line_length:3200
			//printf("location:%d, x:%d, vinfo.xoffset:%d, vinfo.bits_per_pixel/8:%d, y:%d, vinfo.yoffset:%d, finfo.line_length:%d \n",location,x,vinfo.xoffset,vinfo.bits_per_pixel/8,y,vinfo.yoffset,finfo.line_length);
			location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
			y0 = in[j];
			u = in[j + 1] - 128;
			y1 = in[j + 2];
			v = in[j + 3] - 128;

			r = (298 * y0 + 409 * v + 128) >> 8;
			g = (298 * y0 - 100 * u - 208 * v + 128) >> 8;
			b = (298 * y0 + 516 * u + 128) >> 8;

			fb_lcd_buf[ location + 0] = clip(b, 0, 255);
			fb_lcd_buf[ location + 1] = clip(g, 0, 255);
			fb_lcd_buf[ location + 2] = clip(r, 0, 255);
			fb_lcd_buf[ location + 3] = 255;

			r = (298 * y1 + 409 * v + 128) >> 8;
			g = (298 * y1 - 100 * u - 208 * v + 128) >> 8;
			b = (298 * y1 + 516 * u + 128) >> 8;

			fb_lcd_buf[ location + 3] = clip(b, 0, 255);
			fb_lcd_buf[ location + 4] = clip(g, 0, 255);
			fb_lcd_buf[ location + 5] = clip(r, 0, 255);
			fb_lcd_buf[ location + 7] = 255;
		}

		in +=istride;
    }
}
void lcd_display::lcd_init()
{
	int ret = -1;
	lcd_open();
	ret = ioctl(lcd_fd,FBIOGET_FSCREENINFO,&finfo);
	if(ret < 0)
	{
		perror("Fail to ioctl:FBIOGET_FSCREENINFO\n");  
		exit(EXIT_FAILURE); 
	}
	
	ret = ioctl(lcd_fd,FBIOGET_VSCREENINFO,&vinfo);
	
	if(ret < 0)
    {  
		perror("Fail to ioctl:FBIOGET_VSCREENINFO\n");  
		exit(EXIT_FAILURE);  
    }  
    lcd_buf_size = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;  
	fb_lcd_buf = (char *)mmap(NULL,
							lcd_buf_size,
							PROT_READ|PROT_WRITE,
							MAP_SHARED,
							lcd_fd,
							0);
	if(fb_lcd_buf == NULL)
	{
		perror("Fail to mmap\n");  
		exit(EXIT_FAILURE);  
	}
}
#if 0
int main()
{
	char image_data[240*240*2]={0};
	lcd_display t("dev/fb0");
	t.lcd_init();
	t.lcd_show(240,240,image_data);
	return 0;
}
#endif


